#include "GameEngine.h"
#include "GameState.h"
#include "IntroState.h"
#include "PlayState.h"
#include "MenuState.h"
#include "Object.h"
#include "Quadtree.h"
Advanced2D::Map* mapGame;

namespace Advanced2D
{
	CIntroState CIntroState::m_IntroState;
	void CIntroState::Init()
	{
		mapGame=new Advanced2D::Map();
		mapGame->loadTilesTexture("tiles.png");
		Screen.left=Screen.top=0;
		Screen.right=Screen.left+g_engine->getScreenWidth();
		Screen.bottom=Screen.top+g_engine->getScreenHeight();
		mapGame->buildMap();	
	}

	void CIntroState::Cleanup()
	{

	}

	void CIntroState::Pause()
	{

	}

	void CIntroState::Resume()
	{

	}

	void CIntroState::HandleEvents(CGameEngine* game)
	{

	}

	void CIntroState::Update(CGameEngine* game) 

	{
		if (game->getInputHandler()->IsKeyDown(DIK_A))
		{
			game->ChangeState(CMenuState::Instance());
		}

		if (game->getInputHandler()->IsKeyDown(DIK_UPARROW))
		{
			mapGame->getPlayer()->getSprite()->setVelocity(0,-5);
			mapGame->getPlayer()->move();
			mapGame->getPlayer()->animate();
			/*Screen.top-=10;
			Screen.bottom= Screen.top+g_engine->getScreenHeight();*/
			
		}
		if (game->getInputHandler()->IsKeyDown(DIK_DOWNARROW))
		{
			mapGame->getPlayer()->getSprite()->setVelocity(0,5);
			mapGame->getPlayer()->move();
			mapGame->getPlayer()->animate();
			/*Screen.top+=10;
			Screen.bottom=g_engine->getScreenHeight()+Screen.top;*/
			
		}
		if (game->getInputHandler()->IsKeyDown(DIK_LEFTARROW))
		{
			mapGame->getPlayer()->getSprite()->setVelocity(-5,0);
			mapGame->getPlayer()->move();
			mapGame->getPlayer()->animate();
			/*Screen.left-=10;
			Screen.right=Screen.left+g_engine->getScreenWidth();*/

		}

		if (game->getInputHandler()->IsKeyDown(DIK_RIGHTARROW))
		{
			mapGame->getPlayer()->getSprite()->setVelocity(5,0);
			mapGame->getPlayer()->move();
			mapGame->getPlayer()->animate();
			/*Screen.left+=10;
			Screen.right=Screen.left+game->getScreenWidth();*/
		}

		UpdateScreen();
		
		
	
	}

	void CIntroState::UpdateScreen()
	{
		bool OK=false;
		mapGame->getQuadtree()->UpdatePosition(mapGame->getQuadtree()->getTreeRoot(),OK);

		vector<Object*> *temp222=new vector<Object*>();
		mapGame->getQuadtree()->GetAllObj(mapGame->getQuadtree()->_pRoot,*temp222);

		if (mapGame->getPlayer()->getPosition().x>= g_engine->getScreenWidth()/2)
		{
			Screen.left=mapGame->getPlayer()->getPosition().x - (g_engine->getScreenWidth()/2);
			Screen.right=mapGame->getPlayer()->getPosition().x +(g_engine->getScreenHeight()/2);;
		}

		//Screen.left=mapGame->getPlayer()->getPosition().x - (g_engine->getScreenWidth()/2);
		
		if (mapGame->getPlayer()->getPosition().y >= g_engine->getScreenHeight()/2)
		{
			Screen.top=mapGame->getPlayer()->getPosition().y - (g_engine->getScreenHeight()/2);
			Screen.bottom=mapGame->getPlayer()->getPosition().y + (g_engine->getScreenHeight()/2);

		}
		
		
		
 		mapGame->getPlayer()->setIsMoving(false);

	}

	void CIntroState::Draw(CGameEngine* game) 
	{
		game->ClearScene(D3DCOLOR_XRGB(0,0,0));
		mapGame->draw(Screen);
		
	}

}
